package com.cscp.javastudy.basic.reflect;

public class ReflectDemo2 {

    public static void main(String[] args) throws ClassNotFoundException {
        Person p1 = new Student();
        System.out.println(p1.name);
        // 通过对象获得
        Class c1 = p1.getClass();
        // 通过forName获得
        Class c2 = Class.forName("com.cscp.javastudy.basic.reflect.Person");
        System.out.println(c1);
        System.out.println(c2);
        // 通过类名.class
        Class c3 = Student.class;
        System.out.println(c3);
        // 获得父类类型
        Class c4 = c1.getSuperclass();
        System.out.println(c4);

    }

}

class Person{

    String name;

    public Person(String name) {
        this.name = name;
    }

    public Person() {
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

class Student extends Person {

    public Student(){
        this.name = "Student";
    }
}

class Teacher extends Person {

    public Teacher(){
        this.name = "Teacher";
    }
}